package com.huawei.petapp2.community;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.StoreActivity;
import com.huawei.petapp2.entity.Store;

import java.util.List;

public class ShopListAdapter extends BaseAdapter {

    private Context mContext;
    private List<Store> shopList;

    public ShopListAdapter(Context mContext, List<Store> shopList) {
        this.mContext = mContext;
        this.shopList = shopList;
    }

    @Override
    public int getCount() {
        return shopList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder{
        ImageView shop_icon;
        TextView shop_name;
        TextView store_found;
        LinearLayout
                store_show;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        holder = new ViewHolder();
        //View view = LayoutInflater.from(mContext).inflate(R.layout.community_item, parent, false);
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.community_item, parent, false);

            holder.shop_icon = convertView.findViewById(R.id.shop_icon);
            holder.shop_name = convertView.findViewById(R.id.shop_name);
            holder.store_found = convertView.findViewById(R.id.store_found);
            holder.store_show = convertView.findViewById(R.id.store_show);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        ImageView goods1_photo = convertView.findViewById(R.id.goods1);
        TextView goods1_name = convertView.findViewById(R.id.goods1_name);
        TextView goods1_price = convertView.findViewById(R.id.goods1_price);
        ImageView goods2_photo = convertView.findViewById(R.id.goods2);
        TextView goods2_name = convertView.findViewById(R.id.goods2_name);
        TextView goods2_price = convertView.findViewById(R.id.goods2_price);
        ImageView goods3_photo = convertView.findViewById(R.id.goods3);
        TextView goods3_name = convertView.findViewById(R.id.goods3_name);
        TextView goods3_price = convertView.findViewById(R.id.goods3_price);

        if(mContext!=null)
            Glide.with(mContext)
                    .load(shopList.get(position).getStoreimg())
                    .into(holder.shop_icon);

        //  shop_icon.setBackgroundResource(shopList.get(position).getShop_icon());
        holder.shop_name.setText(shopList.get(position).getStorename());
        goods1_name.setText(shopList.get(position).getStorename()+1);
        goods1_price.setText(String.valueOf(100));
        if(mContext!=null)
            Glide.with(mContext)
                    .load(shopList.get(position).getStoreimg2())
                    .into(goods2_photo);
        //  goods2_photo.setBackgroundResource(shopList.get(position).getGoods2().getG_photo());
        goods2_name.setText(shopList.get(position).getStorename()+2);
        goods2_price.setText(String.valueOf(200));
        if(mContext!=null)
            Glide.with(mContext)
                    .load(shopList.get(position).getStoreimg3())
                    .into(goods3_photo);
        // goods3_photo.setBackgroundResource(shopList.get(position).getGoods3().getG_photo());


        goods3_name.setText(shopList.get(position).getStorename()+3);
        goods3_price.setText(String.valueOf(300));
        holder.store_show.setOnClickListener(v -> {
            int storeid=shopList.get(position).getStoreid();
           // Toast.makeText(mContext, "click "+((TextView)v).getText(), Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(mContext.getApplicationContext(), StoreActivity.class);
            intent.putExtra("storeid",storeid);
            mContext.startActivity(intent);

        });

        return convertView;
    }
}
